package MediaOnline.Adapter;


import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import MediaOnline.GUI.R;
import MediaOnline.General.paramConst;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewDebug.FlagToString;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;


public class DownloadedFilmAdapter extends BaseAdapter {
	public Context context;
	//arrListFilm de gan vao gridview
	public static ArrayList<String> arrListFilmName;
	public static ArrayList<String> arrListFilmPath;
	public static ArrayList<Bitmap> arrListFilmThumb;
	
    
    public DownloadedFilmAdapter(Context ctx) {
        context=ctx;
        arrListFilmName = new ArrayList<String>();
        arrListFilmPath = new ArrayList<String>();
        arrListFilmThumb = new ArrayList<Bitmap>();
    }

    public void getListDownloadedFilm(){
    	File dir = new File(paramConst.DownloadedFolder);
        File[] filelist = dir.listFiles();
        if (filelist!=null) {
        	for (File f : filelist) {
        		//Chỉ nhận các file có đuôi là .nhmovie
        		String name = f.getName().toLowerCase();
        		if (name.endsWith(".mp4")) {
        			//Gán phim name
            		String fName = f.getName();
            		fName = fName.substring(0, fName.indexOf('.'));
        			arrListFilmName.add(fName);
        			//Gán phim path
        			arrListFilmPath.add(f.getPath());
        			Log.d("Film path:::::", f.getPath());
        			//Gán image
        			String imgPath = paramConst.DownloadedFolder+"/"+fName+".jpg";
        			Log.d("Image path:::::", imgPath);
        			File imgFile = new  File(imgPath);
        			if(imgFile.exists()){

        			    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        			    arrListFilmThumb.add(myBitmap);
        			}
        			else {
        				arrListFilmThumb.add(null);
        			}
				}
            	
    		}
		}
        
    }
    
    public int getCount() {
        return arrListFilmName.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public void insertValue (){
    	
    }
    

    
    public View getView(final int position, View convertView, ViewGroup parent) {
    	
    	
    	
    	View v = convertView;
        if (v == null) {
        	Log.d("Context::::", context.toString());
        	LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        	convertView = inflater.inflate(R.layout.filmsingle3, null);
        }
        
        //Hiển thị các thông tin cần thiết
        //Thumbail
        ImageView imageView = (ImageView)convertView.findViewById(R.id.FilmSingle3_filmThumb);
        imageView.setScaleType(ScaleType.FIT_CENTER);
        imageView.setPadding(5, 5, 5, 5);
        //Tên phim chính
        TextView txtTenPhimChinh = (TextView)convertView.findViewById(R.id.FilmSingle3_txtTenPhimChinh);
        txtTenPhimChinh.setText(arrListFilmName.get(position));
        //Tên phim phụ
        TextView txtTenPhimPhu = (TextView) convertView.findViewById(R.id.FilmSingle3_txtTenPhimPhu);
        txtTenPhimPhu.setText("");
        //Lay hinh anh
        //Nếu đã có hình ảnh thì load lên image view, không thì lấy ảnh mặc định
        if (arrListFilmThumb.get(position)!=null){
        	imageView.setImageBitmap(arrListFilmThumb.get(position));
		}
		else{
			imageView.setImageResource(R.drawable.noimg);
		}
        //Cắt tên
        String FilmName = arrListFilmName.get(position);
        if(FilmName.length()>40){
        	FilmName = FilmName.substring(0, 37) +"...";
        }
        txtTenPhimChinh.setText(FilmName);
        return convertView;

    }
}